package com.ccb.sc.goods.controller;

import com.ccb.sc.auth.TokenAuth;
import com.ccb.sc.auth.TokenModel;
import com.ccb.sc.goods.controller.query.AppGoodsQueryParam;
import com.ccb.sc.goods.controller.query.AppGoodsSearchParam;
import com.ccb.sc.goods.service.IAppGoodsService;
import com.ccb.sc.goods.vo.AppGoodsListVo;
import com.ccb.sc.goods.vo.AppGoodsVo;
import com.ccb.sc.common.bind.annotation.CurrentUser;
import com.ccb.sc.common.bind.annotation.Limit;
import com.ccb.sc.common.bind.page.PageParams;
import com.ccb.sc.common.bind.page.PageVo;
import com.ccb.sc.common.bind.response.ResBody;
import com.ccb.sc.common.modules.goods.enums.GoodsType;
import com.ccb.sc.common.modules.goods.service.IGoodsCommentService;
import com.ccb.sc.common.modules.goods.vo.GoodsCommentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ken
 * @version 1.0
 * @date 2020-09-21
 */
@RestController
@Api(value = "商品管理", tags = "商品管理")
@RequestMapping("/app/goods")
public class AppGoodsController {

    private IAppGoodsService appGoodsService;
    private IGoodsCommentService goodsCommentService;

    @Limit(period = 10, count = 10)
    @GetMapping("/list")
    @ApiOperation(value = "首页商品列表", response = AppGoodsListVo.class, responseContainer = "list")
    public ResBody<?> list(@Validated AppGoodsQueryParam queryParam) {
        PageVo<AppGoodsListVo> page = this.appGoodsService.list(queryParam);
        return ResBody.success(page);
    }

    @Limit(period = 10, count = 10)
    @GetMapping("/all")
    @ApiOperation(value = "全部商品", response = AppGoodsListVo.class, responseContainer = "list")
    public ResBody<?> all(AppGoodsSearchParam searchParam) {
        PageVo<AppGoodsListVo> page = this.appGoodsService.search(searchParam);
        return ResBody.success(page);
    }

    @Limit(period = 10, count = 10)
    @TokenAuth
    @GetMapping("/exchange")
    @ApiOperation(value = "我能兑换", response = AppGoodsListVo.class, responseContainer = "list")
    public ResBody<?> list(@ApiIgnore  @CurrentUser TokenModel token, AppGoodsSearchParam searchParam) {
        PageVo<AppGoodsListVo> page = this.appGoodsService.exchange(searchParam, token.getId());
        return ResBody.success(page);
    }

    @Limit(period = 10, count = 10)
    @GetMapping("/hot")
    @ApiOperation(value = "人气排行", response = AppGoodsListVo.class, responseContainer = "list")
    public ResBody<?> hot() {
        PageVo<AppGoodsListVo> page = this.appGoodsService.hot();
        return ResBody.success(page);
    }

    @Limit(period = 10, count = 10)
    @GetMapping("/re-stock")
    @ApiOperation(value = "热门待补货", response = AppGoodsListVo.class, responseContainer = "list")
    public ResBody<?> restock(AppGoodsSearchParam searchParam) {
        PageVo<AppGoodsListVo> page = this.appGoodsService.restock(searchParam);
        return ResBody.success(page);
    }

    @Limit(period = 10, count = 10)
    @GetMapping("/new-stock")
    @ApiOperation(value = "新品到货", response = AppGoodsListVo.class, responseContainer = "list")
    public ResBody<?> newStock(AppGoodsSearchParam searchParam) {
        PageVo<AppGoodsListVo> page = this.appGoodsService.newStock(searchParam);
        return ResBody.success(page);
    }

    @Limit(period = 5, count = 3)
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "商品详情", response = AppGoodsVo.class)
    public ResBody<?> detail(@PathVariable Long id,
                             @RequestParam(required = false) String searchKey,
                             @ApiIgnore HttpServletRequest request) {
        AppGoodsVo detailVo = this.appGoodsService.detail(id, request);
        return ResBody.success(detailVo);
    }

    @Limit(period = 5, count = 3)
    @GetMapping("/recommend/list")
    @ApiOperation(value = "推荐列表", response = AppGoodsListVo.class, responseContainer = "list")
    public ResBody<?> recommendList(@RequestParam(required = false) Long goodsId) {
        List<AppGoodsListVo> list = this.appGoodsService.recommendList(goodsId);
        return ResBody.success(list);
    }

    @Limit(period = 5, count = 5)
    @GetMapping("/comments/list")
    @ApiOperation(value = "商品评论列表", response = GoodsCommentVo.class, responseContainer = "list")
    public ResBody<?> commentsList(Long goodsId, PageParams pageParams) {
        PageVo<GoodsCommentVo> page = this.goodsCommentService.comments(goodsId, GoodsType.POINTS_GOODS, pageParams);
        return ResBody.success(page);
    }

    @Autowired
    public void setAppGoodsService(IAppGoodsService appGoodsService) {
        this.appGoodsService = appGoodsService;
    }

    @Autowired
    public void setGoodsCommentService(IGoodsCommentService goodsCommentService) {
        this.goodsCommentService = goodsCommentService;
    }
}
